<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/14
 * Time: 22:33
 */

namespace app\admin\model;


use think\Db;
use think\Model;

class Goodstype extends Model
{
    // 确定链接表名
    protected $name = 'fenlei';



    /**
     * 获取分类数据
     * @return mixed
     */
    public function getGoodsCaseList($where)
    {
        return $this->field('id,name,img,pid')->where($where)->select();
    }

    /**
     * 插入分类信息
     * @param $param
     */
    public function goodscaseAdd($param)
    {
//        var_dump($param);die;
        Db::startTrans();
        try{
            if ($param['pid'] == 0){
                $param['level'] = 1;
            }else{
                $param['level'] = 2;
            }
            if (!$this->insertGetId($param)){
                throw new \Exception('添加失败');
            }else{
                return msg(1, '/admin/goodstype/index', '添加成功');
            }
            Db::commit();
        }catch(PDOException $e){
            Db::rollback();
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑分类
     * @param $param
     */
    public function goodscaseEdie($param)
    {
        try{
            if ($param['pid'] == 0)
            {
                $param['level'] = 0;
            }else{
//                $level= $this->field('level')->find($param['pid']);
                $param['level'] = 1;
            }

            $where['id'] = $param['id'];
            unset($param['file']);
            unset($param['id']);
            if ($this->where($where)->update($param))
            {
                return msg(1, '/admin/goodstype/index', '编辑成功');
            };
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除节点
     * @param $id
     */
    public function goodscaseDel($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除成功');

        }catch(PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }
}